<template>
  <div>
    <van-nav-bar
        title="订单详情"
        left-text="返回"
        left-arrow
        @click-left="back"
    />
    <!--        @click-left="onClickLeft"-->
<!--    1.收获地址-->
    <div>
      <van-cell is-link @click="showPopup"><van-cell center :title="this.test1" :label="this.address1" /></van-cell>
    </div>
    <van-popup style="margin-bottom: 50px" position="bottom" :style="{ height: '50%' }" v-model="show">
      <van-address-list
          v-model="chosenAddressId"
          :list="list"
          default-tag-text="默认"
          @click-item="clickitem"
      />
    </van-popup>


    商品信息
    <div>
      <van-card v-for="(f,index) in orderpre.foods" :key="index"
          :num="f.num"
          :price="f.currentprice"
          :title="f.title"
          :thumb="f.imgurl"
          :origin-price="f.joinprice"
      />
    </div>

    <div>
      <van-cell :border="false" title="可用积分" :value="orderpre.userlevel.score" />
      <van-radio-group v-model="radio"></van-radio-group>
      <van-cell :border="false" title="积分抵扣(元)" :value="this.score" />
      <!-- 优惠券单元格 -->
      <!--    优惠券-->
      <van-coupon-cell
          style="padding-top: 20px;padding-left: 40px;text-align: left"
          :coupons="coupons"
          :chosen-coupon="chosenCoupon"
          @click="coupon"
          icon="coupon-o"
      />
      <!-- 优惠券列表 -->
      <van-popup
          v-model="couponBoolean"
          round
          position="bottom"
          style="height: 90%; padding-top: 4px;"
      >
        <van-coupon-list
            :coupons="coupons"
            :chosen-coupon="chosenCoupon"
            :disabled-coupons="disabledCoupons"
            @change="onChange"
            @exchange="onExchange"
        />
      </van-popup>
    </div>
<!--    5.支付方式-->
    <van-field
        v-model="info"
        type="textarea"
        name="备注"
        placeholder="请输入备注信息"
    />
    <div>
      <van-radio-group v-model="paytype" direction="horizontal">
        <van-radio name="1">支付宝支付</van-radio>
        <van-radio name="2">微信支付</van-radio>
      </van-radio-group>
    </div>
<!--    5.底部-->
    <van-submit-bar :price="allprice*100" button-text="提交订单" @submit="addOrder" />
  </div>
</template>
<script>
export default {
  data(){
    return{
      index1:0,
      chosenAddressId: '1',
      paytype:"2",
      show:false,
      radio: '1',
      address1:"",
      preprice:0,
      info:"",
      orderpre:{
        address:{
        },
        userlevel:{
          score:1290,
          level:1
        },
        foods:[],
        type:2,//记录是商详页1还是购物车2
        cids:[] //记录购物车的id
      },
      score:0,
      allprice:0,
      list: [],
      token:this.$store.state.token,
      //跳出层  默认为关  点击时显示 true  关闭时 false
      couponBoolean:false,

      addressBoolean:false,

      //选择优惠券索引 -1 默认为不选
      chosenCoupon: -1,
      //可用优惠券列表参数
      coupons: [],
      //不可用优惠券列表参数
      disabledCoupons: [],

      couponinfodto:{
        price:1,
        coupid:1,
        coupinfoid:-1
      },
      test1:"",
      no:""
    }
  },mounted() {
      this.axios.get("/api/address/my.do").then(res => {
        this.list = res.data.data;
        console.log(this.list)
        if (this.list.length!=0){
          this.chosenAddressId=1
          console.log(this.list)
          this.test1=this.list[0].name+this.list[0].tel
          this.index1=this.list[0].id;
          this.address1=this.list[0].address
         }
      })
    this.cids = this.$route.query.cids;
    console.log(this.cids)
    var orderpre={
      "type":this.paytype,
      "ids":this.cids
    }
    this.axios.post("/api/order/preview.do",orderpre).then(res=>{
      this.orderpre=res.data.data
      var s=this.orderpre.userlevel.score
      if(s>20){
        this.score=0.2;
      }else {
        this.score=s/100;
      }
      var price=0;
      var food = this.orderpre.foods
      for(var i in food){
        price =price+food[i].num*food[i].currentprice;
      }
      this.couponinfodto.price = price;
      this.allprice = (price-this.score)
      this.preprice = this.allprice;
    })





  },
  methods: {
    showPopup() {
      this.show = true;
    },
    addOrder() {
        var dto={
        "no":Date.parse(new Date())/1000,
        "title":"jjjj",
        "money":0.01,
        "type":1,

      }
      this.no = dto.no;
      this.axios.post("/api/alipay/sendpay.do",dto).then(res=>{
        if(res.data.code==200){
          document.write(res.data.data);
          console.log(this.couponinfodto.coupid)
          this.axios.get("/api/couponInfo/updateCoupon?id="+this.couponinfodto.coupinfoid).then(res=>{
              // !!
          })
          // this.$store.commit("setPay",res.data.data)
          // this.$router.push("/pay")
          var orderdto={
            "paytype":1,
            "no":this.no,
            "info":this.info,
            "type":2,
            "aid":this.index1,
            "cids":this.cids,
            "foods":this.orderpre.foods,
            "score":this.score,
            "couponid":this.couponinfodto.coupid,
          }
          // console.log(this.aid)
          this.axios.post("/api/order/create.do",orderdto).then(r=>{
            if(r.data.code==200){
              document.write(res.data.data);
            }else {
              this.$toast(r.data.msg);
              this.$router.push("/index")
            }
          }
          )
        }
      })
    },
    clickitem(index){

      this.test1=index.name+"   "+index.tel
      this.address1=index.address
      this.index1=index.id
    },
    onClickLeft() {
      this.$router.push("/cart")
    },
    onChange(index) {
      this.couponBoolean = false;
      this.chosenCoupon = index;
      this.$toast("选择优惠券");
      this.axios.get("/api/couponInfo/selectByUid?token=" + this.token).then(res => {
        this.couponinfodto.coupid = res.data.data[this.chosenCoupon].coupid;
        this.axios.get("/api/couponInfo/discountPrice?price=" + this.couponinfodto.price + "&coupid=" + this.couponinfodto.coupid).then(res => {
          if(res.data.code==200){
            this.allprice = this.preprice;
            this.allprice= (res.data.data-this.score)
          }else{
            this.$toast("您的金额不够使用优惠券！！！");
          }
        })

      })

    },
    onExchange(code) {
      this.coupons.push(this.coupons);
      this.allprice=this.preprice
    },
    coupon() {
      this.coupons = [];
      this.couponBoolean = true;
      // this.axios.get("/api/couponInfo/selectByUid?token=" + this.token).then(res => {
      //   if (res.data.code == 200) {
          var a = {}
          // for (var i in res.data.data) {
            this.axios.get("/api/coupon/selectById?token="+ this.token).then(res => {
              console.log(res.data.data)
              if (res.data.code = 200) {
                for (var i in res.data.data){
                    a = {}
                    var ctime = res.data.data[i].ctime;
                    a.condition = res.data.data[i].info;
                    a.reason = '';
                    a.value = res.data.data[i].discountprice * 100;
                    a.name = '优惠券名称';
                    a.startAt = new Date(ctime).getTime();
                    a.endAt = new Date(ctime).getTime() + 60000000;
                    a.valueDesc = res.data.data[i].discountprice;
                    a.unitDesc = '元';
                    this.coupons.push(a)
                }
              }
            })

          // }
      //   }
      // })

    }
    ,
    back(){
      this.$router.push("/cart");
    }

  }
}
</script>
